package cn.nubia.admin.file.bean;

import cn.nubia.admin.application.apk.entity.ApkInfo;
import cn.nubia.admin.application.bean.SoftVO;
import cn.nubia.admin.sysconfig.bean.Category;
import cn.nubia.admin.application.bean.ClientVersion;
import cn.nubia.admin.common.constant.PicSizeEnum;
import cn.nubia.admin.file.FileManager;

import java.io.File;
import java.util.List;

/**
 * Created by justin on 15/7/15.
 */
public class Upload {

    public static final String FILE_DIV = "_";

    private String fileUrl;
    private String localFileUrl;

    private String iconPath;
    private String icon512Url;
    private String icon125Url;
    private String icon114Url;
    private String icon62Url;
    private String iconUrl;
    private String thumbUrl;
    private String adUrl;
    private String adThumbUrl;
    private String screenshotUrl;

    private String localIcon512Url;
    private String localIcon125Url;
    private String localIcon114Url;
    private String localIcon62Url;
    private String localIconUrl;
    private String localThumbUrl;
    private String localAdUrl;
    private String localAdThumbUrl;
    private String localScreenshotUrl;

    private SoftVO softVO = new SoftVO();

    private ClientVersion clientVersion = new ClientVersion();

    private List<Category> secondCatagories;

    private List<Category> thirdCatagories;

    private boolean exist;


    public void initIconPath() {
        String iconPath = FileManager.getLocalDir(FileManager.ABSPATH_APK_ICON);
        this.setIconPath(iconPath);
        this.setLocalIconUrl(iconPath + "icon.png");
        this.setIconUrl(FileManager.getAbsFile(this.getLocalIconUrl()));
    }

    public void initADPath(){
        String adPath = FileManager.getLocalDir(FileManager.ABSPATH_APK_AD);
        this.setLocalAdUrl(adPath + "ad.png");
        this.setAdUrl(FileManager.getAbsFile(this.getLocalAdUrl()));
    }

    /*****
     * 根据解析出来的apk信息初始化
     * @param apkInfo
     */
    public SoftVO initApkInfo(ApkInfo apkInfo) {
        softVO.setPackageName(apkInfo.getPackageName());
        if(apkInfo.getVersionCode()!=null)softVO.setVersionCode(Integer.valueOf(apkInfo.getVersionCode()));
        softVO.setVersionName(apkInfo.getVersionName());
        softVO.setItemName(apkInfo.getApplicationLable());
        String userPermistions = "";
        for(String permission : apkInfo.getUsesPermissions()){
            userPermistions += permission + ";";
        }
        softVO.setPermission(userPermistions);
        if(apkInfo.getMinSdkVersion() != null){
            softVO.setMinFirewareId(Integer.valueOf(apkInfo.getMinSdkVersion()));
        }else {
            if(apkInfo.getSdkVersion() != null){
                softVO.setMinFirewareId(Integer.valueOf(apkInfo.getSdkVersion()));
            }
        }
        if(apkInfo.getMaxSdkVersion()!=null){
            softVO.setMaxFirewareId(Integer.valueOf(apkInfo.getMaxSdkVersion()));
        }else{
            if(apkInfo.getTargetSdkVersion()!=null){
                softVO.setMaxFirewareId(Integer.valueOf(apkInfo.getTargetSdkVersion()));
            }
        }
        if(softVO.getSoftName()==null){
            softVO.setSoftName(apkInfo.getApplicationLable());
        }
        softVO.setFileMD5(apkInfo.getFileMD5());
        softVO.setFileSize(apkInfo.getFileSize());
        String supportsScreens = apkInfo.getSupportsScreens();
        int ret = 0;
        if (supportsScreens.contains("small"))
        {
            ret = ret|2;
        }
        if (supportsScreens.contains("normal"))
        {
            ret = ret|4;
        }
        if (supportsScreens.contains("large"))
        {
            ret = ret|8;
        }
        if (supportsScreens.contains("xlarge"))
        {
            ret = ret|16;
        }
        softVO.setResolution(ret);
        return this.getSoftVO();
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public String getLocalFileUrl() {
        return localFileUrl;
    }

    public void setLocalFileUrl(String localFileUrl) {
        this.localFileUrl = localFileUrl;
    }

    public String getIconPath() {
        return iconPath;
    }

    public void setIconPath(String iconPath) {
        this.iconPath = iconPath;
    }

    public String getIcon512Url() {
        return icon512Url;
    }

    public void setIcon512Url(String icon512Url) {
        this.icon512Url = icon512Url;
    }

    public String getIcon125Url() {
        return icon125Url;
    }

    public void setIcon125Url(String icon125Url) {
        this.icon125Url = icon125Url;
    }

    public String getIcon114Url() {
        return icon114Url;
    }

    public void setIcon114Url(String icon114Url) {
        this.icon114Url = icon114Url;
    }

    public String getIcon62Url() {
        return icon62Url;
    }

    public void setIcon62Url(String icon62Url) {
        this.icon62Url = icon62Url;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
        if(this.iconUrl!=null&&!"".equals(this.iconUrl)){
            int location = iconUrl.lastIndexOf(File.separator);
            if(location>0){
                String iconPath = iconUrl.substring(0, location);
                this.setIcon512Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_512.getSize() + ".png");
                this.setIcon125Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_125.getSize() + ".png");
                this.setIcon114Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_114.getSize() + ".png");
                this.setIcon62Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_62.getSize() + ".png");
            }
        }
    }

    public String getThumbUrl() {
        return thumbUrl;
    }

    public void setThumbUrl(String thumbUrl) {
        this.thumbUrl = thumbUrl;
    }

    public String getAdUrl() {
        return adUrl;
    }

    public void setAdUrl(String adUrl) {
        this.adUrl = adUrl;
        if(adUrl!=null && adUrl!="") {
            int location = adUrl.lastIndexOf(File.separator);
            if(location>0){
                String adUrlPath = adUrl.substring(0, location);
                this.setAdThumbUrl(adUrlPath + File.separator + "adthumb.png");
            }
        }
    }

    public String getAdThumbUrl() {
        return adThumbUrl;
    }

    public void setAdThumbUrl(String adThumbUrl) {
        this.adThumbUrl = adThumbUrl;
    }

    public String getScreenshotUrl() {
        return screenshotUrl;
    }

    public void setScreenshotUrl(String screenshotUrl) {
        this.screenshotUrl = screenshotUrl;
    }

    public String getLocalIcon512Url() {
        return localIcon512Url;
    }

    public void setLocalIcon512Url(String localIcon512Url) {
        this.localIcon512Url = localIcon512Url;
    }

    public String getLocalIcon125Url() {
        return localIcon125Url;
    }

    public void setLocalIcon125Url(String localIcon125Url) {
        this.localIcon125Url = localIcon125Url;
    }

    public String getLocalIcon114Url() {
        return localIcon114Url;
    }

    public void setLocalIcon114Url(String localIcon114Url) {
        this.localIcon114Url = localIcon114Url;
    }

    public String getLocalIcon62Url() {
        return localIcon62Url;
    }

    public void setLocalIcon62Url(String localIcon62Url) {
        this.localIcon62Url = localIcon62Url;
    }

    public String getLocalIconUrl() {
        return localIconUrl;
    }

    public void setLocalIconUrl(String localIconUrl) {
        this.localIconUrl = localIconUrl;
        if(this.localIconUrl!=null||!"".equals(this.localIconUrl)){
            int location = localIconUrl.lastIndexOf(File.separator);
            if(location>0){
                String iconPath = localIconUrl.substring(0, location);
                this.setLocalIcon512Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_512.getSize() + ".png");
                this.setLocalIcon125Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_125.getSize() + ".png");
                this.setLocalIcon114Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_114.getSize() + ".png");
                this.setLocalIcon62Url(iconPath + File.separator + "icon" +FILE_DIV + PicSizeEnum.ICON_62.getSize() + ".png");
            }
        }

    }

    public String getLocalThumbUrl() {
        return localThumbUrl;
    }

    public void setLocalThumbUrl(String localThumbUrl) {
        this.localThumbUrl = localThumbUrl;
    }

    public String getLocalAdUrl() {
        return localAdUrl;
    }

    public void setLocalAdUrl(String localAdUrl) {
        this.localAdUrl = localAdUrl;
        if(localAdUrl!=null && localAdUrl!="") {
            int location = localAdUrl.lastIndexOf(File.separator);
            if(location>0){
                String adUrlPath = localAdUrl.substring(0, location);
                this.setLocalAdThumbUrl(adUrlPath + File.separator + "adthumb.png");
            }
        }
    }

    public String getLocalAdThumbUrl() {
        return localAdThumbUrl;
    }

    public void setLocalAdThumbUrl(String localAdThumbUrl) {
        this.localAdThumbUrl = localAdThumbUrl;
    }

    public String getLocalScreenshotUrl() {
        return localScreenshotUrl;
    }

    public void setLocalScreenshotUrl(String localScreenshotUrl) {
        this.localScreenshotUrl = localScreenshotUrl;
    }

    public SoftVO getSoftVO() {
        return softVO;
    }

    public void setSoftVO(SoftVO softVO) {
        this.softVO = softVO;
    }

    public List<Category> getSecondCatagories() {
        return secondCatagories;
    }

    public void setSecondCatagories(List<Category> secondCatagories) {
        this.secondCatagories = secondCatagories;
    }

    public List<Category> getThirdCatagories() {
        return thirdCatagories;
    }

    public void setThirdCatagories(List<Category> thirdCatagories) {
        this.thirdCatagories = thirdCatagories;
    }

    public ClientVersion getClientVersion() {
        return clientVersion;
    }

    public void setClientVersion(ClientVersion clientVersion) {
        this.clientVersion = clientVersion;
    }

    public boolean isExist() {
        return exist;
    }

    public void setExist(boolean exist) {
        this.exist = exist;
    }
}
